﻿using System;
using System.Device.Location;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Platform;
using _160Projects.LetMeFillThatOutForYouServiceReference;
using Thread = System.Threading.Thread;

namespace CapstoneMockup.ViewModels
{
    public class GpsModel
    {
        public GeoCoordinate FirstPosition { get; private set; }
        const double EarthRadius = 3958.75587;
        public double TotalMileage { get; private set; }
        public readonly GeoCoordinateWatcher GeoCoordinateWatcher;
        private readonly Thread _thread;
        public int Counter = 1000;
        public LocationCollection locations = new LocationCollection();
        public MapLayer MyRouteLayer { get; private set; }
        public GeoCoordinate CurrentLocation { get; private set; }
        public int counter = 0;
        public GeoCoordinate TheirLocation { get; set; } // Used for the location compairson page

        //private Service1Client proxy;

        public GpsModel()
        {


            //proxy.StoreLocationCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_StoreLocationCompleted);
            GeoCoordinateWatcher = new GeoCoordinateWatcher();
            
            GeoCoordinateWatcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High) { MovementThreshold = 100 };

            var routeColor = Colors.Blue;
            var routeBrush = new SolidColorBrush(routeColor);
            var routeLine = new MapPolyline();
            routeLine.Locations = locations;
            routeLine.Stroke = routeBrush;
            routeLine.Opacity = 0.65;
            routeLine.StrokeThickness = 5.0;

            MyRouteLayer = new MapLayer();
            MyRouteLayer.Children.Add(new MapLayer());
            CurrentLocation = new GeoCoordinate();
        }

        public void Stop()
        {
            if (!double.IsNaN(CurrentLocation.Latitude)&&!double.IsNaN(CurrentLocation.Longitude))
            {
                decimal longitude = unchecked((decimal) CurrentLocation.Longitude);
                decimal latitude = unchecked((decimal) CurrentLocation.Latitude);
                GeoCoordinateWatcher.Stop();
                App.ViewModel.WcfConnection.StoreLocationAsync(App.ViewModel.User.Id, longitude, latitude,
                                                               new DateTime());
                App.ViewModel.WcfConnection.LogMilageAsync(App.ViewModel.User.Id, (decimal) TotalMileage);
            }
            else
            {
                GeoCoordinateWatcher.Stop();
                App.ViewModel.WcfConnection.LastMilageAsync(App.ViewModel.User.Id,(decimal) TotalMileage);
            }
        }

        public void UpdateLocation(GeoPositionChangedEventArgs<GeoCoordinate> e)
        {
            if (FirstPosition == null||counter%2 == 1)
            {
                FirstPosition = e.Position.Location;
            }

            double a = Math.Pow(Math.Sin((((FirstPosition.Latitude * Math.PI) / 180) - ((e.Position.Location.Latitude * Math.PI) / 180)) / 2), 2) + Math.Cos((FirstPosition.Latitude * Math.PI) / 180) * Math.Cos(((e.Position.Location.Latitude * Math.PI) / 180)) * (Math.Pow(Math.Sin((((FirstPosition.Longitude * Math.PI) / 180) - ((e.Position.Location.Longitude * Math.PI) / 180)) / 2), 2));
            double c = (2 * (Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a))));
            double d = EarthRadius * c;
            TotalMileage += d;

            CurrentLocation = e.Position.Location;

            App.ViewModel.WcfConnection.StoreLocationAsync(App.ViewModel.User.Id, (decimal) e.Position.Location.Longitude, (decimal)e.Position.Location.Latitude, e.Position.Timestamp.DateTime);
        }
    }
}
